<?php
include_once(dirname(__FILE__) . "/../../configure/configure.php");		//--> global var
include_once(dirname(__FILE__) . "/../configure/admin.config.inc.php");	//--> admin global var
include_once(dirname(__FILE__) . "/../tools/smarty/libs/Smarty.class.php");					//--> out template

require_once(dirname(__FILE__) . "/../controlHeader.php");					//--> system control header
require_once(dirname(__FILE__) . "/../controlOperate.php");				//-->

include_once("impl/ContentImpl.php");
include_once("impl/ImageImpl.php");
include_once("utils/FileUtils.php");

	
$contImpl 	= new ContentImpl(DB_TAG_MM);
$imgImpl	= new ImageImpl(DB_TAG_MM);

if(isset($_GET['id']) && !empty($_GET['id'])){
	$id = $_GET['id'];
	
	$oImages = $contImpl->getImages($id);
	
	$isOk = $contImpl->delete($id);
	if($isOk){
		foreach($oImages as $oImg){
			if($contImpl->isNotExisted($id, $oImg->id)){
				$isOk = $imgImpl->delete($oImg->id);
				if($isOk){
					$path = FileUtils::getFullPathByID(IMAGE_PATH_CONTENT, $oImg->id, $oImg->id . '.' . $oImg->extName);
					$error_message = "delete path: $path,";
					
					if (file_exists($path)) {
						unlink($path);					
					}
				}
			}else
				$error_message = "Image id $id other content used. delete fail. ";
		}
		
		$error_message .= 'delete content and all images of the content.';
	}else 
		$error_message = 'delete failure.';
}


echo "<script language='javascript'>";
echo "alert(\"$error_message\");";
echo "location.href=\"".$_SERVER['HTTP_REFERER']."\";";
echo "</script>";

//$backurl = $_SERVER['HTTP_REFERER'];
//header("Location: $backurl");
?>
